//
//  UserModel.m
//  ZYOnlineSchool
//
//  Created by 毕武侠 on 2017/2/15.
//  Copyright © 2017年 bwx. All rights reserved.
//

#import "UserModel.h"
#import "JPUSHService.h"

@implementation UserModel

+ (instancetype)shareInstance {
    static UserModel *userinfo = nil;
    static dispatch_once_t oncePredicate;
    dispatch_once(&oncePredicate, ^{
        NSString *path = [[UserModel class] getUserPath];
        NSLog(@"path = %@", path);
        NSData *data = [[NSMutableData alloc] initWithContentsOfFile: path];
        NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
        userinfo = [[UserModel alloc] initWithCoder:unarchiver];
        [unarchiver finishDecoding];
    }); 
    return userinfo;
}

MJCodingImplementation

- (NSString *)uid {
    if (_id == nil) {
        _id = @"";
    }
    return _id;
}

+ (NSString *)getUserPath {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    return [documentsDirectory stringByAppendingPathComponent:kArchivingFileKey];
}

#pragma mark - save 

- (void)emptyInfo {
    
    self.id = @"";
    self.middle_img = @"";
    self.phone = @"";
    self.attention_num = @"";
    self.fans_num = @"";
    self.small_img = @"";
    self.type = @"";
    self.big_img = @"";
    self.name = @"";
    self.status = @"";
    self.token = @"";
    self.is_centify = @"";


    [JPUSHService setTags:[NSSet set] alias:self.id fetchCompletionHandle:^(int iResCode, NSSet *iTags, NSString *iAlias) {
        NSLog(@"iResCode:%d iTags:%@ , iAlias:%@",iResCode,iTags,iAlias);
    }];

}

- (void)copyWithModel:(UserModel *)model {
    if (model) {
        
        self.id = [model.id copy];
        self.middle_img = [model.middle_img copy];
        self.phone = [model.phone copy];
        self.attention_num = [model.attention_num copy];
        self.fans_num = [model.fans_num copy];
        self.small_img = [model.small_img copy];
        self.type = [model.type copy];
        self.big_img = [model.big_img copy];
        self.name = [model.name copy];
        self.status = [model.status copy];
        self.token = [model.token copy];
        self.is_centify = [model.is_centify copy];
        
        
        [JPUSHService setTags:[NSSet set] alias:self.id fetchCompletionHandle:^(int iResCode, NSSet *iTags, NSString *iAlias) {
            //NSLog(@"iResCode:%d iTags:%@ , iAlias:%@",iResCode,iTags,iAlias);
            [self updataJPUSHServiceSetAliasSuccess];
        }];
    }
}

- (void)updataJPUSHServiceSetAliasSuccess{
//    NSString *url = [NSString stringWithFormat:@"%@%@",[OtherNetworking sharedManager].STRURL,@"Member/is_user_message"];
//    SGParameter *parameter = [[SGParameter alloc] init];
//    [SGHttpTool POST:url parameter:parameter sucess:^(id result) {
//        //NSLog(@"result success :%@",result);
//    } failure:^(id reslut) {
//        //NSLog(@"result failure :%@",reslut);
//    }];
}
- (void)saveUserInfo {
    NSMutableData *data = [[NSMutableData alloc] init];
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
    [self encodeWithCoder:archiver];
    [archiver finishEncoding];
    [data writeToFile:[[UserModel class] getUserPath] atomically:YES];
}

@end
